﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace QLNS.Models
{
    public class QHGiaDinhModels
    {
        
        
        public static List<QHGD> GetDSQHGDTheoCanBo(int id)
        {
                   
            //khoi tao db
            ITAPMNS2013Entities context = new ITAPMNS2013Entities();

            var query = from dt in context.DSQHGD
                        where dt.IDCanBo == id
                        select dt;
            return query.ToList<QHGD>();
        }
        
        public static QHGD GetChiTietDSQHGD(int id)
        {
            //khoi tao db
            ITAPMNS2013Entities context = new ITAPMNS2013Entities();
            var query = (from s in context.DSQHGD
                         where s.ID == id
                         select s).FirstOrDefault();
            return query;

        }

        public static List<MyModels> GetQHGDList()
        {
            ITAPMNS2013Entities content = new ITAPMNS2013Entities();
            var query = from qhgd in content.DSQHGD
                        join cb in content.DSCanBo on qhgd.IDCanBo equals cb.ID
                        orderby qhgd.ID descending
                        select new MyModels { qhgd = qhgd, cb = cb };
            return query.ToList();
        }
         
        public static void Add(QHGD qhgd)
        {
            ITAPMNS2013Entities context = new ITAPMNS2013Entities();
            context.DSQHGD.AddObject(qhgd);
            context.SaveChanges();
        }
        public static void Delete(int id)
        {
            ITAPMNS2013Entities context = new ITAPMNS2013Entities();
            var query = (from h in context.DSQHGD
                         where h.ID == id
                         select h).FirstOrDefault();

            context.DSQHGD.DeleteObject(query);
            context.SaveChanges();

        }
        public static void DeleteAllOfPerson(int id)
        {
            ITAPMNS2013Entities context = new ITAPMNS2013Entities();
            var query = from h in context.DSQHGD
                        where h.IDCanBo == id
                        select h;
            foreach (var item in query)
            {
                context.DSQHGD.DeleteObject(item);
            }
            context.SaveChanges();
        }
        public static void Update(QHGD id)
        {
            ITAPMNS2013Entities context = new ITAPMNS2013Entities();
            var query = from h in context.DSQHGD
                        where h.ID == id.ID
                        select h;
            QHGD kq = query.First<QHGD>();
            kq = id;
            context.DSQHGD.ApplyCurrentValues(kq);
            context.SaveChanges();

        }
        //sua dan toc
        public static void Edit(QHGD id)
        {
            ITAPMNS2013Entities context = new ITAPMNS2013Entities();
            var query = from h in context.DSQHGD
                        where h.ID == id.ID
                        select h;
            QHGD kq = query.First<QHGD>();
            kq = id;
            context.DSQHGD.ApplyCurrentValues(kq);
            context.SaveChanges();

        }
    }
}